
package general.semantics;

import general.art.CenteredCluster;
import general.art.Cluster;
import general.art.Link;
import general.art.Node;
import general.art.NodeTypes;
import general.database.SQLiteInterface;
import general.exceptions.NodeNotFoundException;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.SortedSet;

import art.applications.seek.Result;
import art.applications.seek.Seeker;
import art.applications.seek.Status;
import art.applications.seek.implementations.SimpleSeeker;


/**
 * This class does all of the communication between both ART and the DB. The front end should only use this
 * search class to get any of the required functionalities and 
 * 
 * @author Hood, Lance
 *
 */
public class WeightSearch {

    /** String to print in the current seed node searched in Art.   */
    protected static String sFinishPopulatingTxt = "Finished population";

    /** String to print in the current seed node searched in Art.   */
    protected String mAvilabelNodes = "available nodes: ";

    /** String to print in the current seed node searched in Art.  */
    protected String mPrintSeedNode;

    /**
	 * Max no of iterations.
	 */
	protected static final int NO_OF_ITTERATIONS = 5;

	/**
	 * This is the weight representing the Positive seeding nodes in Art.
	 */
	protected static final double POSITIVE_WEIGHT = 1.0;

	/** String builder to hold the string written by ART's seeker. */
	private StringBuilder mSb = new StringBuilder();

	/** Simple seeker that implements the Seeker class in ART. The simple seeker
	 * provides all of the functionalities needed by the server.
	 * */
	private SimpleSeeker<NodeTypes> mSeeker;

	/** This holds a sorted set of results from ART. */
	private SortedSet<Result<NodeTypes>> mRes;

	/** The iterator that holds the results from ART. */
	private Iterator<Result<NodeTypes>> mResItr;

    /**
     * This method  populates ART with all of the links for a particular seeding node.
     * 
     * @param seedingNodes   List of seeding nodes' keys.
     * @param weights       List of seeding nodes' weights.
     */
    public void preformQuery(final CenteredCluster c){
        
    	/* create the seeker we are going to use */
		mSeeker    =  new SimpleSeeker<NodeTypes>(mSb, Seeker.Verbosity.SIMPLE, NO_OF_ITTERATIONS);
		mSeeker.setStatus(c.getSeedNode(), Status.POSITIVE);
        for (Link l : c.getLinks())
		{
			mSeeker.addLink(l.getNode1(), l.getNode2());
		}
        
        mRes = mSeeker.seek();
		mResItr = mRes.iterator();

		
        
    }

    public boolean hasNext(){
    	
    	return mResItr.hasNext();
    	
    }
    
    /**
     * @return Node     Next node in the result
     */
    public Result<NodeTypes> nextResult() {

		return  mResItr.next();
		
    }

    public static void main (String [] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, NodeNotFoundException{
    
    	CenteredCluster myCluster = new CenteredCluster(34065, 3);
    	WeightSearch mySearch = new WeightSearch();
    	
    	System.out.println(myCluster.getDepth());
    	
    	mySearch.preformQuery(myCluster);
    	
    	while (mySearch.hasNext())
    	{
    	
    		Result<NodeTypes> r = mySearch.nextResult();
    		System.out.println(r.node() + " " + r.weight() );
    		
    		
    	}
    	
    	System.out.println("Finnished");
    }
    
}
